package com.simons.module.configure;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.DefaultSessionStorageEvaluator;
import org.apache.shiro.mgt.DefaultSubjectDAO;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.mgt.DefaultSessionManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.web.filter.authc.AnonymousFilter;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;

import com.simons.module.shiro.StatelessDefaultSubjectFactory;
import com.simons.module.shiro.StatelessRealm;


/**
 * @author jsm
 */
public abstract class ShiroConfig {


    /**
     * 无状态域注入
     * @return
     */
    @Bean
    public StatelessRealm statelessRealm(){
       return this.getStatelessRealm();
    }
    
    /**
     * 实现者想要获得shiro的功能，先实现这个config然后重写这个获取state热身赛Realm的方法
     * @return
     */
    public abstract StatelessRealm getStatelessRealm();

    /**
     * 会话管理类 禁用session
     * @return
     */
    @Bean
    public DefaultSessionManager defaultSessionManager(){
        DefaultSessionManager manager = new DefaultSessionManager();
        manager.setSessionValidationSchedulerEnabled(false);
        return manager;
    }

    /**
     * 安全管理类
     * @param statelessRealm
     * @return
     */
    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager(StatelessRealm statelessRealm){
        DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
        //禁用sessionStorage
        DefaultSubjectDAO de = (DefaultSubjectDAO)manager.getSubjectDAO();
        DefaultSessionStorageEvaluator defaultSessionStorageEvaluator =(DefaultSessionStorageEvaluator)de.getSessionStorageEvaluator();
        defaultSessionStorageEvaluator.setSessionStorageEnabled(false);

        manager.setRealm(statelessRealm);

        //无状态主题工程，禁止创建session
        StatelessDefaultSubjectFactory statelessDefaultSubjectFactory = new StatelessDefaultSubjectFactory();
        manager.setSubjectFactory(statelessDefaultSubjectFactory);

        manager.setSessionManager(defaultSessionManager());
        //设置了SecurityManager采用使用SecurityUtils的静态方法 获取用户等
        SecurityUtils.setSecurityManager(manager);
        return manager;
    }

    /**
     * Shiro生命周期处理
     * @return
     */
    @Bean
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor(){
        return new LifecycleBeanPostProcessor();
    }



    /**
     *
     * @Function: ShiroConfig::anonymousFilter
     * 该过滤器无需增加 shiro默认会添加该filter
     */
    public AnonymousFilter anonymousFilter(){
        return new AnonymousFilter();
    }




    /**
     * shiro注解
     * @param securityManager
     * @return
     */
    @Bean //如果屏蔽controller权限注解接入可注释掉这个
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor
                = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }

}